package com.example.demo.controller;

import com.example.demo.constant.Constant;
import com.example.demo.pojo.TokenData;
import com.example.demo.pojo.User;
import com.example.demo.service.LoginService;
import com.example.demo.utils.JwtUtil;
import com.example.demo.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录Controller
 */
@RestController
@ResponseBody
@CrossOrigin
@RequestMapping("/lgtout")
public class LoginController {

    @Autowired
    private LoginService loginService;

    private static Logger logger =  LoggerFactory.getLogger(LoginController.class);

    //1-正常,2暂停,3-挂失,4-注销 state (√)
    @RequestMapping(value="/login",method = RequestMethod.POST)
    public Result login(User user){
        Result result = this.loginService.login(user);
        //logger.info("登录结果：" + result);
        if(result.getStatus() == 200){
            //登录成功
            //1.创建token
            String jwt = JwtUtil.createJWT(Constant.JWT_ID, JwtUtil.generalSubject(result.getData()), Constant.JWT_TTL);
            TokenData data = (TokenData) result.getData();
            // logger.info("token : " + jwt);
            Map<String,Object> map = new HashMap<>();
            map.put("token", jwt);
            map.put("user_status",data.getUser_state());
            map.put("state",data.getState());
            map.put("user_name",data.getUser_name());
            result.setData(map);
            return result;
        }
        return result;


    }




    public void test(){

    }
}
